package org.esti.security;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.esti.model.User;
import org.esti.service.UserService;
import org.esti.utils.VelocitySecUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;

/**
 * @author Artem Loginov (logart)
 */
@Component
@Aspect
public class AddUserAvatarAspect {
    @Autowired
    private UserService userService;

    @Pointcut("within(org.esti.controller.*) && execution(String *(.., org.springframework.ui.Model, ..)) && args(model)")
    public void renderPage(Model model) {
    }

    @After("renderPage(model)")
    public void addAvatarLink(Model model) {
        String username = VelocitySecUser.getPrincipal();
        if (null != username) {
            User user = userService.getUserByLogin(username);
            model.addAttribute("userAvatarLink", user.getAvatar());
        }

    }


}
